#include <stdio.h>

int f(long long A, long long B, long long n);

int main()
{
    long long A, B, n;
    long long i, address;
    long long soup[48] = {0};
    while(scanf("%ld%ld%ld", &A, &B, &n)==3)
    {
        if(A==0 && B==0 && n==0)
        {
            break;
        }
        soup[1] = 1;
        soup[2] = 1;
        for (i = 3; i < 48;i++)
        {
            soup[i] = (A * soup[i - 1] + B * soup[i - 2]) % 7;
        }
        address = n % 48;
        printf("%ld\n", soup[address]);
    }
    return 0;
}